package com.tjcu.service.impl;

import com.tjcu.dao.UserDao;
import com.tjcu.entity.User;
import com.tjcu.service.UserService;
import com.tjcu.utils.EmailUtils;
import com.tjcu.utils.MybatisUtil;

import java.sql.SQLException;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/10/24 23:17
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class UserServiceImpl implements UserService {
    @Override
    public boolean checkUser(String username) {
       return false;

    }

    @Override
    public void register(User user) throws SQLException {
        UserDao mapper = (UserDao) MybatisUtil.getMapper(UserDao.class);
        //先注册
        mapper.insertUser(user);
        //发送邮件
        EmailUtils.sendEmail(user);
        MybatisUtil.commit();
    }

    /**
     * @param code 根据激活码进行激活
     * @return
     * @throws SQLException
     */
    @Override
    public int activeUser(String code) throws SQLException {
        UserDao mapper = (UserDao) MybatisUtil.getMapper(UserDao.class);
        //1.根据激活码查找用户 0：未激活 1.激活 2. 已经激活了的
        User user = mapper.selectByCode(code);
        System.out.println(user);
        if(user==null){
            return 0;
        }
        //2.判断用户是否激活
       if(user.getStatus().equals(1)){
           return 2;
       }
        System.out.println(user.getStatus());
        System.out.println(user.getId());

        //3.进行激活操作
       mapper.updateStatusByUid(1,user.getId());
        MybatisUtil.commit();
           return 1;

    }

    @Override
    public User showByUsernameAndPassword(String username, String password) {
        UserDao mapper = (UserDao) MybatisUtil.getMapper(UserDao.class);
        User user = mapper.selectByUsernameAndPassword(username, password);
        return user;
    }

}
